package com.atguigu.lease.web.app.interceptors;

import com.atguigu.lease.common.exceptions.LeaseException;
import com.atguigu.lease.common.jwts.JwtUtil;
import com.atguigu.lease.common.result.ResultCodeEnum;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.web.servlet.HandlerInterceptor;

/**
 * @Author zhaolingqiao
 * @CreateTime 2024/12/15
 */
@Component
public class AppLoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String accessToken = request.getHeader("access_token");
        //判断是否携带token
        if(ObjectUtils.isEmpty(accessToken)){
            //未携带token
            throw new LeaseException(ResultCodeEnum.APP_LOGIN_AUTH);
        }
        //校验token，如果不合法，方法内部报异常，返回
        JwtUtil.parseToken(accessToken);

        return true;
    }
}
